﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using ESOL.AutoUpdate.EsolWebService;
using System.IO;
using System.Net;

namespace ESOL.AutoUpdate
{
    public partial class frmUpdateNew : frmBaseForm
    {
        private string mProcesseName = "ESOL.WinUI";
        private string mLocalFilePath = string.Empty;
        private bool mCompletedDownload = false;

        public frmUpdateNew()
        {
            InitializeComponent();
        }

        public void StartDownload()
        {
            try
            {
                mLocalFilePath = Application.StartupPath;
                if (Directory.Exists(mLocalFilePath) == false)
                {
                    Directory.CreateDirectory(mLocalFilePath);
                }
                //下载
                using (EsolService service = new EsolService())
                {
                    string[] fileNames = service.GetUpdateFiles();
                    if (fileNames != null && fileNames.Length > 0)
                    {
                        for (int i = 0; i < fileNames.Length; i++)
                        {
                            using (WebClient client = new WebClient())
                            {
                                string fileName = fileNames[i].Substring(fileNames[i].LastIndexOf("\\") + 1);
                                string url = service.Url.Replace("/EsolService.asmx", "") + "/" + service.GetUpdateFolderName() + "/" + fileName;
                                string localFileName = mLocalFilePath + "\\" + fileName;
                                Uri u = new Uri(url);
                                //client.DownloadFileAsync(u, localFileName);
                                client.DownloadFile(u, localFileName);
                            }
                        }
                    }
                }
                Thread.Sleep(1000);
                mCompletedDownload = true;

                Thread.Sleep(1000);
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("更新失败！\n" + ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void frmUpdateNew_Load(object sender, EventArgs e)
        {
            System.Diagnostics.Process[] wmsProcess = System.Diagnostics.Process.GetProcessesByName(mProcesseName);
            foreach (System.Diagnostics.Process process in wmsProcess)
            {
                process.Kill();
            }
        }

        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);

            StartDownload();
        }

        private void frmUpdateNew_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (mCompletedDownload == false)
            {
                DialogResult result = MessageBox.Show("程序正在升级中，强行退出可能造成程序错误，确认要退出吗？", "警告", MessageBoxButtons.OKCancel);
                if (result == DialogResult.Cancel)
                {
                    e.Cancel = true;
                }
            }
        }
    }
}
